//
//  CodeView.swift
//  手账
//
//  Created by zhuxiaoxiang on 2018/12/14.
//  Copyright © 2018年 zhuxiaoxiang. All rights reserved.
//

import UIKit

@IBDesignable class CodeView: UIView {
    // 验证码字符串
    var changeString: String?
    
    let kLineCount = 6
    let kLineWidth = CGFloat(1.0)
    // 字符串个数
    let kCharCount = 4
    // 字符串字体大小
    let kFontSize = UIFont.systemFont(ofSize: (CGFloat(arc4random() % 5) + 18))
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.backgroundColor = UIColor.randomColor
        generateCode()
        self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(updateCode)))
    }

}

extension CodeView {
    
    @objc func updateCode() {
        generateCode()
        self.setNeedsDisplay()
    }
    
    func generateCode() {
        // 字符素材数组
        let generateArray: NSArray = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
        changeString = ""
        for _ in 0 ... kCharCount-1 {
            let index = Int(arc4random()) % (generateArray.count - 1)
            let getStr = generateArray.object(at: index)
            changeString = changeString! + (getStr as! String)
        }
    }
    
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        
        self.backgroundColor = UIColor.randomColor
        
        guard let changeString = changeString else {
            return
        }
        
        let str = NSString(string: "S")
        let size = str.size(withAttributes: [NSAttributedStringKey.font: kFontSize])
        let width: CGFloat = rect.size.width / CGFloat(NSString(string: changeString).length) - size.width
        let height = rect.size.height - size.height
        var point: CGPoint?
        var pX: CGFloat?
        var pY: CGFloat?
        
        let stringCount: Int = changeString.count
        
        for i in 0 ... stringCount-1 {
            pX = CGFloat(arc4random()).truncatingRemainder(dividingBy: width) + rect.size.width / CGFloat(NSString(string: changeString).length) * CGFloat(i)
            pY = CGFloat(arc4random()).truncatingRemainder(dividingBy: height)
            
            point = CGPoint(x: pX!, y: pY!)
            let c = NSString(string: changeString).character(at: i)
            let codeText: NSString? = NSString(format: "%C", c)
            codeText!.draw(at: point!, withAttributes: [NSAttributedStringKey.font: kFontSize, NSAttributedStringKey.foregroundColor: UIColor.randomColor])
        }
        
        let context: CGContext = UIGraphicsGetCurrentContext()!
        context.setLineWidth(kLineWidth)
        for _ in 0 ... kLineCount - 1 {
            let randomLineColor = UIColor.randomColor
            context.setStrokeColor(randomLineColor.cgColor)
            pX = CGFloat(arc4random()).truncatingRemainder(dividingBy: rect.size.width)
            pY = CGFloat(arc4random()).truncatingRemainder(dividingBy: rect.size.height)
            context.move(to: CGPoint(x: pX!, y: pY!))
            
            pX = CGFloat(arc4random()).truncatingRemainder(dividingBy: rect.size.width)
            pY = CGFloat(arc4random()).truncatingRemainder(dividingBy: rect.size.height)
            context.addLine(to: CGPoint(x: pX!, y: pY!))
            
            context.strokePath()
        }
    }
}
